#include <SFGUI/SFGUI.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFGUI/Engines/BREW.hpp>

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

class HelloWorld {
	public:
		// Our button click handler.
		void OnButtonClick();

		void Run();

	private:
		// Create an SFGUI. This is required before doing anything with SFGUI.
		sfg::SFGUI m_sfgui;

		// Create the label pointer here to reach it from OnButtonClick().
		sfg::Label::Ptr m_label;
		sfg::Frame::Ptr window;
		sf::Vector2f window_size;
		sf::Vector2f frame_req;
};

void HelloWorld::OnButtonClick() {
	m_label->SetText( "Hello SFGUI, pleased to meet you!" );
}

void HelloWorld::Run() {
	// Create SFML's window.
	sf::RenderWindow render_window( sf::VideoMode( SCREEN_WIDTH, SCREEN_HEIGHT ), "Hello world!" );

	// Create the label.
	m_label = sfg::Label::Create( "Hello world!" );
	// Create a simple button and connect the click signal.
	sfg::Button::Ptr button( sfg::Button::Create( "Greet SFGUI!" ) );
	button->GetSignal( sfg::Widget::OnLeftClick ).Connect( &HelloWorld::OnButtonClick, this );

    sfg::Box::Ptr toolbar(sfg::Box::Create());
    sfg::Button::Ptr button_box(sfg::Button::Create("coucou"));
    //button_box->GetSignal( sfg::Widget::OnLeftClick ).Connect( &HelloWorld::OnButtonClick, this );
    sfg::Button::Ptr button_box2(sfg::Button::Create("lalilou"));
    sfg::Button::Ptr button_box3(sfg::Button::Create("dadidou"));
    toolbar->Pack( button_box2 );
    toolbar->Pack( button_box3);
    toolbar->Pack( button_box );
    sfg::Window::Ptr letoolbar(sfg::Window::Create());
    letoolbar->SetStyle(sfg::Window::BACKGROUND);
    letoolbar->Add(toolbar);

    sf::Image leimage;
    leimage.loadFromFile("caca.png");
    sfg::Image::Ptr image(sfg::Image::Create(leimage));
    image->SetRequisition(sf::Vector2f(SCREEN_WIDTH,SCREEN_HEIGHT));

	// Create a vertical box layouter with 5 pixels spacing and add the label
	// and button to it.
	sfg::Box::Ptr box( sfg::Box::Create( sfg::Box::VERTICAL, 0.0f ) );
	box->Pack( m_label,false );
	box->Pack( button, false );
	sfg::Separator::Ptr sep(sfg::Separator::Create(sfg::Separator::VERTICAL));
	sep->Show(false);
	box->Pack(sep);

	// Create a window and add the box layouter to it. Also set the window's title.
	sfg::Window::Ptr window( sfg::Window::Create() );
	window->SetTitle( "Hello world!" );
	window->SetStyle(sfg::Window::BACKGROUND
                  | sfg::Window::SHADOW);
	window->Add( box );

	// Create a desktop and add the window to it.
	sfg::Desktop desktop;
	sfg::Context::Get().GetEngine().SetProperty(m_label->GetName(),"BackgroundColor",sf::Color::White);
	desktop.Add(image);
	desktop.Add( window );
	desktop.Add(letoolbar);
	// toolbar requisition
    window_size = (sf::Vector2f) render_window.getSize();
    frame_req = window->GetRequisition();
	letoolbar->SetAllocation(sf::FloatRect(0,
                                        window_size.y-letoolbar->GetRequisition().y,
                                        window_size.x-frame_req.x,
                                        letoolbar->GetRequisition().y));
    frame_req = window->GetRequisition();
    //window->SetRequisition(sf::Vector2f(frame_req.x,window_size.y * 0.98));
    window->SetAllocation(sf::FloatRect(window_size.x-frame_req.x,
                          0,
                          frame_req.x,
                          window_size.y));
	// We're not using SFML to render anything in this program, so reset OpenGL
	// states. Otherwise we wouldn't see anything.
	render_window.resetGLStates();

	// Main loop!
	sf::Event event;
	sf::Clock clock;

	while( render_window.isOpen() ) {
		// Event processing.
		while( render_window.pollEvent( event ) ) {
			desktop.HandleEvent( event );

			// If window is about to be closed, leave program.
			if( event.type == sf::Event::Closed ) {
				render_window.close();
			}

			if( event.type == sf::Event::Resized ) {
                window_size = (sf::Vector2f) render_window.getSize();
                window->SetAllocation(sf::FloatRect(window_size.x-window->GetRequisition().x,
                          0,
                          window->GetRequisition().x,
                          window_size.y));
                letoolbar->SetAllocation(sf::FloatRect(0,
                                         window_size.y-letoolbar->GetRequisition().y,
                                         window_size.x-window->GetRequisition().x,
                                         letoolbar->GetRequisition().y));
			}
		}

		// Update SFGUI with elapsed seconds since last call.
		desktop.Update( clock.restart().asSeconds() );



		// Rendering.
		render_window.clear();
		m_sfgui.Display( render_window );
		render_window.display();
	}
}

int main() {
	HelloWorld hello_world;
	hello_world.Run();

	return 0;
}
